﻿using QuanLyKho.DAL;
using QuanLyKho.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyKho.GUI
{
    public partial class NhapForm : Form
    {
        public NhapForm()
        {
            InitializeComponent();
        }

        private void NhapForm_Load(object sender, EventArgs e)
        {
            initControls();
            generateMaPhieu();
        }

        private void generateMaPhieu()
        {
            int soLuongPhieu = new PhieuNhapDAO().getListPhieuNhap().Count;
            string maPhieu = "PN" + soLuongPhieu;
            textBoxMaPhieu.Text = maPhieu;
        }

        private void initControls()
        {
            reloadHangComboBox();
            reloadNhaPhanPhoiComboBox();
            reloadKhoCombox();
        }

        private void reloadKhoCombox()
        {
            KhoDAO khoDAO = new KhoDAO();
            ArrayList listKho = khoDAO.getListKho();
            if (listKho.Count == 0)
            {
                KhoDTO emptyKho = new KhoDTO();
                emptyKho.MaKho = "";
                emptyKho.TenKho = "";
                listKho.Add(emptyKho);
            }
            cmbMaKho.DisplayMember = "MaKho";
            cmbMaKho.ValueMember = "TenKho";
            cmbMaKho.DataSource = listKho;
        }

        private void reloadNhaPhanPhoiComboBox()
        {
            NhaPhanPhoiDAO nhaPhanPhoiDAO = new NhaPhanPhoiDAO();
            ArrayList listNhaPhanPhoi = nhaPhanPhoiDAO.GetListNhaPhanPhoi();
            if (listNhaPhanPhoi.Count == 0)
            {
                NhaPhanPhoiDTO emptyNhaPhanPhoi = new NhaPhanPhoiDTO();
                emptyNhaPhanPhoi.Ma = "";
                emptyNhaPhanPhoi.Ten = "";
                listNhaPhanPhoi.Add(emptyNhaPhanPhoi);
            }
            cmbNhaPhanPhoi.DisplayMember = "Ma";
            cmbNhaPhanPhoi.ValueMember = "Ten";
            cmbNhaPhanPhoi.DataSource = listNhaPhanPhoi;
        }

        private void reloadHangComboBox()
        {
            HangHoaDAO hangHoaDAO = new HangHoaDAO();
            ArrayList listHangHoa = hangHoaDAO.GetListHang();
            if (listHangHoa.Count == 0)
            {
                HangHoaDTO emptyHangHoa = new HangHoaDTO();
                emptyHangHoa.MaHang = "";
                emptyHangHoa.TenHang = "";
                listHangHoa.Add(emptyHangHoa);
            }
            cmbMaHang.DisplayMember = "MaHang";
            cmbMaHang.ValueMember = "TenHang";
            cmbMaHang.DataSource = listHangHoa;
        }

        private void cmbMaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMaHang.SelectedIndex >= 0)
            {
                textBoxTenHang.Text = cmbMaHang.SelectedValue.ToString();
            }
        }

        private void btnThemHang_Click(object sender, EventArgs e)
        {
            ThemMoiHangForm themMoiHangFrm = new ThemMoiHangForm();
            themMoiHangFrm.ShowDialog();
            reloadHangComboBox();
        }

        private void btnNhaPhanPhoi_Click(object sender, EventArgs e)
        {
            ThemMoiNhaPhanPhoiForm themMoiNhaPhanPhoiFrm = new ThemMoiNhaPhanPhoiForm();
            themMoiNhaPhanPhoiFrm.ShowDialog();
            reloadNhaPhanPhoiComboBox();
        }

        private void cmbNhaPhanPhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNhaPhanPhoi.SelectedIndex >= 0)
            {
                textBoxTenNhaPhanPhoi.Text = cmbNhaPhanPhoi.SelectedValue.ToString();
            }
        }

        private void cmbMaKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbMaKho.SelectedIndex >= 0)
            {
                textBoxTenKho.Text = cmbMaKho.SelectedValue.ToString();
            }
        }

        private void btnThemKho_Click(object sender, EventArgs e)
        {
            ThemMoiKhoForm themMoiKhoFrm = new ThemMoiKhoForm();
            themMoiKhoFrm.ShowDialog();
            reloadKhoCombox();
        }

        private void buttonThemHang_Click(object sender, EventArgs e)
        {
            if (textBoxMaPhieu.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập mã phiếu", "Cảnh báo");
                return;
            }

            if (textBoxSoLuong.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập số lượng", "Cảnh báo");
                return;
            }

            //Cập nhật phiếu nhập
            PhieuNhapDTO phieuNhapDTO = new PhieuNhapDTO();
            phieuNhapDTO.MaPhieu = textBoxMaPhieu.Text;
            phieuNhapDTO.MaKho = cmbMaKho.Text;
            phieuNhapDTO.MaNhaPhanPhoi = cmbNhaPhanPhoi.Text;
            phieuNhapDTO.MaHang = cmbMaHang.Text;
            phieuNhapDTO.NgayNhap = datePickerNgayNhap.Value;
            phieuNhapDTO.SoLuong = Int32.Parse(textBoxSoLuong.Text);

            try
            {
                PhieuNhapDAO phieuNhapDAO = new PhieuNhapDAO();
                phieuNhapDAO.insert(phieuNhapDTO);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi thêm sản phẩm vào phiếu xuất kho", "Lỗi");
                return;
            }

            reloadDanhSachHangDataGridView();
        }

        private void reloadDanhSachHangDataGridView()
        {
            if (textBoxMaPhieu.Text != "")
            {
                PhieuNhapDAO phieuNhapDAO = new PhieuNhapDAO();
                ArrayList listHangPhieuNhap = phieuNhapDAO.getListHangTrongPhieuNhapByMa(textBoxMaPhieu.Text);
                dataGridViewChiTietPhieuNhap.DataSource = listHangPhieuNhap;
            }
        }
    }
}
